#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# QcmNumberPad Copyright (C) 2009  Chris Marsh
# Version 0.2.0
#
# Currency Converter is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or any later version.
#
# This program is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# *********************************************************************************
# Original inspiration taken from wwWidgets QwwNumPad.
# http://www.wysota.eu.org/wwwidgets/
# Copyright © 2007-2009 by Witold Wysota
# *********************************************************************************

from PyQt4.QtCore import SIGNAL,  Qt,  QString,  QSize
from PyQt4.QtGui import QFrame,  QButtonGroup,  QGridLayout,  QToolButton,  QSizePolicy,  QIcon,  QPixmap
import QcmNumberPad_rc

# *********************************************************************************

class QcmNumberPad(QFrame):

    __pyqtSignals__ = ("numberClicked(int)",  "decimalClicked()",
                                  "enterClicked()",  "backspaceClicked()", 
                                  "clearClicked()" )
        
    def __init__(self, isPopup=False,  parent = None):
        super(QcmNumberPad, self).__init__(parent)
        
        if isPopup:
            self.setWindowFlags(Qt.Popup)
            self.setFrameStyle(QFrame.StyledPanel | QFrame.Raised)
        
        self.__deleteOnClose = False
        self.setFrameStyle(QFrame.StyledPanel | QFrame.Raised)
        
        layout = QGridLayout(self)
        layout.setSpacing(0)
        layout.setMargin(0)
        group = QButtonGroup(self)
        
        for i in range(9):
            button = self.createButton(QString.number(i+1))
            layout.addWidget(button, i/3, i%3)
            group.addButton(button, i+1)
    
        zeroButton = self.createButton(text="0")
        layout.addWidget(zeroButton, 3, 0)
        group.addButton(zeroButton, 0)
        
        pointButton = self.createButton(text=".")
        layout.addWidget(pointButton,  3,  1)
        group.addButton(pointButton,  10)
        
        enterButton = self.createButton(icon=":/enter.png")
        layout.addWidget(enterButton,  3, 2, 1, 2)
        group.addButton(enterButton,  11)
        
        backspaceButton = self.createButton(icon=":/back.png")
        layout.addWidget(backspaceButton,  1, 3, 2, 1)
        group.addButton(backspaceButton,  12)
        
        clearButton = self.createButton (text="C")
        layout.addWidget(clearButton,  0, 3)
        group.addButton(clearButton,  13)
        
        self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        self.setMaximumSize(QSize(1, 1))
        
        self.connect(group, SIGNAL("buttonClicked(int)"), self.buttonClicked)
    # __init__
        
    def closeEvent(self,  event):
        if self.__deleteOnClose:
            self.deleteLater()
        event.accept()
    # closeEvent
    
    def deleteOnClose(self):
        return self.__deleteOnClose
    # deleteOnClose
    
    def setDeleteOnClose(self,  deleteOnClose):
        self.__deleteOnClose = deleteOnClose
    # setDeleteOnClose
    
    def createButton(self,  text=None,  icon=None):
        button = QToolButton()
        button.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding)
        if not text == None:
            button.setText(text)
        if not icon == None:
            button.setIcon(QIcon(QPixmap(icon)))
        return button
    # createButton

    def buttonClicked(self,  id):
        if (id >= 0 and id < 10):
            self.emit(SIGNAL("numberClicked(int)"), (id))
            self.emit(SIGNAL("keyClicked(QString&)"),  (QString.number(id)))
            return
            
        if (id == 10):
            self.emit(SIGNAL("decimalClicked"),  ())
            self.emit(SIGNAL("keyClicked(QString&)"),  ("."))
            
        if (id == 11):
            self.emit(SIGNAL("enterClicked()"),  ())
            self.close()
            return
            
        if (id == 12):
            self.emit(SIGNAL("backspaceClicked()"),  ())
            return
            
        if (id == 13):
            self.emit(SIGNAL("clearClicked()"),  ())
            return
    # buttonClicked

if __name__ == "__main__":
    # Debug and test widget
    import sys
    from PyQt4.QtGui import QApplication
    app = QApplication(sys.argv)
    numberPad = QcmNumberPad()
    numberPad.show()
    sys.exit(app.exec_())
